import serial
import datetime
import crcmod
import time
import struct

#|FF03|长度|


#firstreturn  = 'FF036200FF25800000000000000000000000000000000000AA00013FCCCCCD3F8000003F9C28F600000000000000023FCCCCCD3F8000003F9C28F600000000000000053FCCCCCD3F8000003F9C28F600000000000000000000000000000000000000000000FF2F'
firstreturn  =  'FF036200FF25800000000000000000000000000000000000AA00153FCCCCCD3F8000003F9C28F600000000000000023FCCCCCD3F8000003F9C28F600000000000000053FCCCCCD3F8000003F9C28F600000000000000000000000000000000000000000000DED3'
secondreturn =  'FF036200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000A80'
makesecond =    'FF036200FF00000000000000000000000000000000000000AA00013FCCCCCD3F8000003F9C28F600000000000000023FCCCCCD3F8000003F9C28F600000000000000053FCCCCCD3F8000003F9C28F600000000000000000000000000000000000000000000EEE6'

new1st =  'ff03640001258000010001000100000000000000000000000000003f800000000000000000000000030000000000003f800000000000000000000000030000000000003f8000000000000000000000000300000000000000000000000000000000000000000000c6d1'
new2nd =  'ff03640001000000010001000100000000000000000000000000003f800000000000000000000000030000000000003f800000000000000000000000030000000000003f80000000000000000000000003000000000000000000000000000000000000000000000AD7'

std1st =  'ff03640001258000010001000100000000000000000000000000003f800000000000003F80000000000000000000003f800000000000004000000000030000000000003f8000000000000040400000000300000000000000000000000000000000000000000000617F'
std2nd =  'ff03640000000000010001000100000000000000000000000000003f800000000000004080000000030000000000003f8000000000000040a0000000000000000000003f8000000000000040c0000000000000000000000000000000000000000000000000000082F8'
cg1st  =  'ff03640001258000010001000100000000000000000000000000153f800000000000003F80000000000000000000153f800000000000004000000000030000000000153f8000000000000040400000000300000000000000000000000000000000000000000000B102'
cg2nd  =  'ff03640000000000010001000100000000000000000000000000153f800000000000004080000000030000000000153f8000000000000040a0000000000000000000153f8000000000000040c000000000000000000000000000000000000000000000000000005285'

cg1_1 =   'ff0364000125800001000100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000404051EC0003000000000000000000000000000000000000000000005DA6'
cg1_2 =   'ff03640001258000010001000100000000000000000000000000003f800000000000003F80000000000000000000003f800000000000004000000000030000000000003f80000000000000404000000004000000000000000000000000000000000000000000000769'

dbg1_1 =  'ff0364000996000001000100010000000a000000000000000000153f68f5c30ad7233c0000000000030000000000153f6b851e0ad7a33c3ec7e35600030000000000153f6e147b90c2f53c3ec946ea000300000000000000000000000000000000000000000000b96f'
dbg2_1 =  'ff03640000000000010001000100000000000000000000000000153f70a3d60ad7233d3ec7e35600030000000000153f733333cdcc4c3d3ec7b6e400030000000000153f75c29090c2753d000000000003000000000000000000000000000000000000000000002288'

dbg1_4_10='ff036400014b00000100000001000000000000000000000000000f683fc3f54f3f295c3f98d17c000100020000000d3f8f5c293df5c2903f860b9600000002000000013f90a3d73e051eb83f9afd140002000200000000000000000000000000000000000000007f4c'
dbg2_4_10='ff03640000000000000001000000000000000000000000000000173f91eb853e0f5c293f89d7e000000002000000113f9333333e19999a3f9cf11e000200020000001a3f947ae13e23d70a3f89d7e0000000020000000000000000000000000000000000000000fe0e'

notused_1='ff036400014b000001000000010000000000000000000000000015683fc3f54f3f295c3f98d17c00010002000000153f8f5c293df5c2903f860b9600000002000000153f90a3d73e051eb83f9afd14000200020000000000000000000000000000000000000000'
notused_2='ff03640000000000010001000100000000000000000000000000153f800000000000004080000000030000000000153f8000000000000040a0000000000000000000153f8000000000000040c00000000000000000000000000000000000000000000000000000'
port = 'COM5'
bps = 9600
timeout = 5

def crc16(datain):
    crc = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0xFFFF, xorOut=0x0000)
    ansint = crc(datain)
    ans = int.to_bytes(ansint, length=2, byteorder='big', signed=False)
    return ans[0], ans[1]



def withCrc(datain):
    dt = bytes.fromhex(datain)
    _crc = crc16(dt)
    _rvcrc = (_crc[1], _crc[0])
    return (dt + bytes(_rvcrc)).hex()


firstframe = withCrc(notused_1)
secndframe = withCrc(notused_2)





try:
    com = serial.Serial(port=port, baudrate=bps)
    com.close()
    com.open()
    print('串口打开')
    while True:
        if com.in_waiting:
            databytes = com.read(com.in_waiting)
            data = databytes.hex()
            print(datetime.datetime.now(), '接收', data)
            if data == 'ff0300000032d1c1':
                com.write(bytes().fromhex(firstframe))
                print(datetime.datetime.now(),'第一帧', firstframe)
            elif data == 'ff0300320032700e':
                com.write(bytes().fromhex(secndframe))
                print(datetime.datetime.now(), '第二帧', secndframe)
            elif data == 'ff06000200003dd4':
                print('静音')
                com.write(bytes().fromhex('ff06000200003DD4'))
            elif data == 'ff0600020001fc14':
                print('解除静音')
                com.write(bytes().fromhex('ff0600020001fc14'))
            elif databytes[1] == 0x10:
                print('写寄存器')
                towrite = bytearray(8)
                towrite[0] = 0xff
                towrite[1] = 0x10
                towrite[2] = databytes[2]
                towrite[3] = databytes[3]
                towrite[4] = databytes[4]
                towrite[5] = databytes[5]
                result = crc16(towrite[0:6])
                towrite[6] = result[1]
                towrite[7] = result[0]
                print(towrite.hex())
                com.write(towrite)
            elif data == 'ff410064000da801':
                print('6位密码读')
                com.write(bytes().fromhex('FF416406383838383838000000006BA0'))
            elif data == 'ff41012c000d29eb':
                print('6位时间读')
                com.write(bytes().fromhex('FF41000001E5E8'))
            elif data == 'ff4100c8000d6820':
                print('8位密码读')
                com.write(bytes().fromhex('ff41640838383838383838380000d3e5'))
            elif data == 'ff410190000de80f':
                print('8位时间读')
                com.write(bytes.fromhex('FF41000001E5E8'))
            elif data == 'ff06000300006c14':
                com.write(bytes().fromhex('ff06000300006c14'))
            elif data == '':
                pass
except Exception as e:
    print("异常", e)

